package com.wx.app.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.newemptyapplication.R;
import com.squareup.picasso.Picasso;
import com.wx.app.mylib.app.BaseActivity;
import com.wx.app.mylib.app.Globals;
import com.wx.app.mylib.app.LocalData;
import com.wx.app.mylib.dialog.PhotoDialog;
import com.wx.app.mylib.utils.BitmapUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class OkHttpActivity extends BaseActivity {

    //    private TextView textView;
//    private ImageView imageView;
    private PhotoDialog mPhotoDialog;

    @BindView(R.id.tv_ok_show)
    TextView tvShow;
    @BindView(R.id.iv_ok)
    ImageView ivShow;
    @BindView(R.id.btn_download)
    Button btnDownload;
    @BindView(R.id.btn_request)
    Button btnRequest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_http);

        ButterKnife.bind(mActivity);
//        textView = (TextView) findViewById(R.id.tv_ok_show);
//        imageView = (ImageView) findViewById(R.id.iv_ok);
        downloadString();
        downloadImage();
        test();
    }

    @OnClick(R.id.btn_request)
    void request() {
        OkHttpUtils.post()
                .url("")
                .addParams(sidKey, sidValue)
                .addParams("sid", LocalData.CacheKey.sessionID)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        mActivity.mLocalData.putString("", "");

                    }
                });


    }


    @OnClick(R.id.btn_download)
    void download() {
        OkHttpUtils
                .post()
                .url("")
                .build()
                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "zhb.apk") {
                    @Override
                    public void inProgress(float progress, long total) {

                    }

                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(File response) {

                    }
                });

    }


    private void test() {
        mPhotoDialog = new PhotoDialog(mActivity);
        mPhotoDialog.show();

//        OkHttpUtils.post()
//                .build()
//                .execute(new FileCallBack() {
//                    @Override
//                    public void inProgress(float progress, long total) {
//
//                    }
//
//                    @Override
//                    public void onError(Call call, Exception e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(File response) {
//
//                    }
//                });

    }

    private void downloadImage() {
        String url = "http://7xl2ga.com1.z0.glb.clouddn.com/banner00.png";
        OkHttpUtils.post().url(url)
                .build().execute(new BitmapCallback() {
            @Override
            public void onError(Call call, Exception e) {

            }

            @Override
            public void onResponse(Bitmap response) {
                ivShow.setImageBitmap(BitmapUtils.toRoundBitmap(response));
            }
        });
        //设置本地图片到imageView
//        Picasso.with(mActivity).load(R.drawable.bg_cert_border).into(ivShow);
        Picasso.with(mActivity).load(url).into(ivShow);
    }

    public void downloadString() {
        String url = "http://7xl2ga.com1.z0.glb.clouddn.com/two_guess01.txt";
        OkHttpUtils.post()
                .url(Globals.domain + url)
//                .params()
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        JSONObject obj = JSONObject.parseObject(response);
//                        textView.setText(response);
                        JSONArray array = obj.getJSONArray("goodlist");
                        JSONObject object = array.getJSONObject(0);
//                        textView.setText(object.toJSONString());
                        tvShow.setText("显示结果：" + object.getString("title"));
                        showToast("显示结果：" + object.getString("title"));
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PhotoDialog.REQUEST_TAKE) {
            Uri path = mPhotoDialog.getPicUri();
            if (path != null && new File(mPhotoDialog.getPicPath()).exists()) {
                mPhotoDialog.cropImage(path);
            }
        } else if (requestCode == PhotoDialog.REQUEST_IMPORT || requestCode == PhotoDialog.REQUEST_CAMERA) {
            if (data != null) {
                mPhotoDialog.cropImage(data.getData());
            }
        } else if (PhotoDialog.REQUEST_CROP_PIC == requestCode) {
            if (data == null)
                return;
            //裁剪返回
            Bitmap bitmap = data.getParcelableExtra("data");
            if (bitmap != null) {
                String imagePath = mPhotoDialog.getSaveBitmapPath(bitmap);
//                upload(imagePath, bitmap);
                showToast("上传图片。。。。。。。。。");

            } else {
                mActivity.showToast("请检查您的sd卡是否可以使用");
            }
        }

    }
}
